home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Libraries / tcl7.4b3 / doc / expr.n < prev    next >
Encoding:
Text File  |  1995-02-22  |  10.6 KB  |  299 lines

  1. '\"
  2. '\" Copyright (c) 1993 The Regents of the University of California.
  3. '\" Copyright (c) 1994-1995 Sun Microsystems, Inc.
  4. '\"
  5. '\" See the file "license.terms" for information on usage and redistribution
  6. '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  7. '\" 
  8. '\" @(#) expr.n 1.9 95/02/22 14:37:23
  9. '\" 
  10. .so man.macros
  11. .HS expr tcl 7.4
  12. .BS
  13. '\" Note:  do not modify the .SH NAME line immediately below!
  14. .SH NAME
  15. expr \- Evaluate an expression
  16. .SH SYNOPSIS
  17. \fBexpr \fIarg \fR?\fIarg arg ...\fR?
  18. .BE
  19.  
  20. .SH DESCRIPTION
  21. .PP
  22. .VS
  23. Concatenates \fIarg\fR's (adding separator spaces between them),
  24. evaluates the result as a Tcl expression, and returns the value.
  25. .VE
  26. The operators permitted in Tcl expressions are a subset of
  27. the operators permitted in C expressions, and they have the
  28. same meaning and precedence as the corresponding C operators.
  29. Expressions almost always yield numeric results
  30. (integer or floating-point values).
  31. For example, the expression
  32. .DS
  33. \fBexpr 8.2 + 6\fR
  34. .DE
  35. evaluates to 14.2.
  36. Tcl expressions differ from C expressions in the way that
  37. operands are specified.  Also, Tcl expressions support
  38. non-numeric operands and string comparisons.
  39. .SH OPERANDS
  40. .PP
  41. A Tcl expression consists of a combination of operands, operators,
  42. and parentheses.
  43. White space may be used between the operands and operators and
  44. parentheses; it is ignored by the expression processor.
  45. Where possible, operands are interpreted as integer values.
  46. Integer values may be specified in decimal (the normal case), in octal (if the
  47. first character of the operand is \fB0\fR), or in hexadecimal (if the first
  48. two characters of the operand are \fB0x\fR).
  49. If an operand does not have one of the integer formats given
  50. above, then it is treated as a floating-point number if that is
  51. possible.  Floating-point numbers may be specified in any of the
  52. ways accepted by an ANSI-compliant C compiler (except that the
  53. ``f'', ``F'', ``l'', and ``L'' suffixes will not be permitted in
  54. most installations).  For example, all of the
  55. following are valid floating-point numbers:  2.1, 3., 6e4, 7.91e+16.
  56. If no numeric interpretation is possible, then an operand is left
  57. as a string (and only a limited set of operators may be applied to
  58. it).
  59. .PP
  60. Operands may be specified in any of the following ways:
  61. .IP [1]
  62. As an numeric value, either integer or floating-point.
  63. .IP [2]
  64. As a Tcl variable, using standard \fB$\fR notation.
  65. The variable's value will be used as the operand.
  66. .IP [3]
  67. As a string enclosed in double-quotes.
  68. The expression parser will perform backslash, variable, and
  69. command substitutions on the information between the quotes,
  70. and use the resulting value as the operand
  71. .IP [4]
  72. As a string enclosed in braces.
  73. The characters between the open brace and matching close brace
  74. will be used as the operand without any substitutions.
  75. .IP [5]
  76. As a Tcl command enclosed in brackets.
  77. The command will be executed and its result will be used as
  78. the operand.
  79. .IP [6]
  80. .VS
  81. As a mathematical function whose arguments have any of the above
  82. forms for operands, such as ``\fBsin($x)\fR''.  See below for a list of defined
  83. functions.
  84. .VE
  85. .LP
  86. Where substitutions occur above (e.g. inside quoted strings), they
  87. are performed by the expression processor.
  88. However, an additional layer of substitution may already have
  89. been performed by the command parser before the expression
  90. processor was called.
  91. As discussed below, it is usually best to enclose expressions
  92. in braces to prevent the command parser from performing substitutions
  93. on the contents.
  94. .PP
  95. For some examples of simple expressions, suppose the variable
  96. \fBa\fR has the value 3 and
  97. the variable \fBb\fR has the value 6.
  98. Then the command on the left side of each of the lines below
  99. will produce the value on the right side of the line:
  100. .DS
  101. .ta 6c
  102. \fBexpr 3.1 + $a    6.1
  103. expr 2 + "$a.$b"    5.6
  104. expr 4*[llength "6 2"]    8
  105. expr {{word one} < "word $a"}    0\fR
  106. .DE
  107. .SH OPERATORS
  108. .PP
  109. The valid operators are listed below, grouped in decreasing order
  110. of precedence:
  111. .TP 20
  112. \fB\-\0\0+\0\0~\0\0!\fR
  113. .VS
  114. Unary minus, unary plus, bit-wise NOT, logical NOT.  None of these operands
  115. .VE
  116. may be applied to string operands, and bit-wise NOT may be
  117. applied only to integers.
  118. .TP 20
  119. \fB*\0\0/\0\0%\fR
  120. Multiply, divide, remainder.  None of these operands may be
  121. applied to string operands, and remainder may be applied only
  122. to integers.
  123. .VS
  124. The remainder will always have the same sign as the divisor and
  125. an absolute value smaller than the divisor.
  126. .VE
  127. .TP 20
  128. \fB+\0\0\-\fR
  129. Add and subtract.  Valid for any numeric operands.
  130. .TP 20
  131. \fB<<\0\0>>\fR
  132. Left and right shift.  Valid for integer operands only.
  133. .TP 20
  134. \fB<\0\0>\0\0<=\0\0>=\fR
  135. Boolean less, greater, less than or equal, and greater than or equal.
  136. Each operator produces 1 if the condition is true, 0 otherwise.
  137. These operators may be applied to strings as well as numeric operands,
  138. in which case string comparison is used.
  139. .TP 20
  140. \fB==\0\0!=\fR
  141. Boolean equal and not equal.  Each operator produces a zero/one result.
  142. Valid for all operand types.
  143. .TP 20
  144. \fB&\fR
  145. Bit-wise AND.  Valid for integer operands only.
  146. .TP 20
  147. \fB^\fR
  148. Bit-wise exclusive OR.  Valid for integer operands only.
  149. .TP 20
  150. \fB|\fR
  151. Bit-wise OR.  Valid for integer operands only.
  152. .TP 20
  153. \fB&&\fR
  154. Logical AND.  Produces a 1 result if both operands are non-zero, 0 otherwise.
  155. Valid for numeric operands only (integers or floating-point).
  156. .TP 20
  157. \fB||\fR
  158. Logical OR.  Produces a 0 result if both operands are zero, 1 otherwise.
  159. Valid for numeric operands only (integers or floating-point).
  160. .TP 20
  161. \fIx\fB?\fIy\fB:\fIz\fR
  162. If-then-else, as in C.  If \fIx\fR
  163. evaluates to non-zero, then the result is the value of \fIy\fR.
  164. Otherwise the result is the value of \fIz\fR.
  165. The \fIx\fR operand must have a numeric value.
  166. .LP
  167. See the C manual for more details on the results
  168. produced by each operator.
  169. All of the binary operators group left-to-right within the same
  170. precedence level.  For example, the command
  171. .DS
  172. \fBexpr 4*2 < 7\fR
  173. .DE
  174. returns 0.
  175. .PP
  176. The \fB&&\fP, \fB||\fP, and \fB?:\fP operators have ``lazy
  177. evaluation'', just as in C, 
  178. which means that operands are not evaluated if they are
  179. not needed to determine the outcome.  For example, in the command
  180. .DS
  181. \fBexpr {$v ? [a] : [b]}\fR
  182. .DE
  183. only one of \fB[a]\fR or \fB[b]\fR will actually be evaluated,
  184. depending on the value of \fB$v\fP.  Note, however, that this is
  185. only true if the entire expression is enclosed in braces;  otherwise
  186. the Tcl parser will evaluate both \fB[a]\fR and \fB[b]\fR before
  187. invoking the \fBexpr\fR command.
  188. .SH "MATH FUNCTIONS"
  189. .PP
  190. .VS
  191. Tcl supports the following mathematical functions in expressions:
  192. .DS
  193. .ta 3c 6c 9c
  194. \fBacos\fR    \fBcos\fR    \fBhypot\fR    \fBsinh\fR
  195. \fBasin\fR    \fBcosh\fR    \fBlog\fR    \fBsqrt\fR
  196. \fBatan\fR    \fBexp\fR    \fBlog10\fR    \fBtan\fR
  197. \fBatan2\fR    \fBfloor\fR    \fBpow\fR    \fBtanh\fR
  198. \fBceil\fR    \fBfmod\fR    \fBsin\fR
  199. .DE
  200. Each of these functions invokes the math library function of the same
  201. name;  see the manual entries for the library functions for details
  202. on what they do.  Tcl also implements the following functions for
  203. conversion between integers and floating-point numbers:
  204. .TP
  205. \fBabs(\fIarg\fB)\fI
  206. Returns the absolute value of \fIarg\fR.  \fIArg\fR may be either
  207. integer or floating-point, and the result is returned in the same form.
  208. .TP
  209. \fBdouble(\fIarg\fB)\fR
  210. If \fIarg\fR is a floating value, returns \fIarg\fR, otherwise converts
  211. \fIarg\fR to floating and returns the converted value.
  212. .TP
  213. \fBint(\fIarg\fB)\fR
  214. If \fIarg\fR is an integer value, returns \fIarg\fR, otherwise converts
  215. \fIarg\fR to integer by truncation and returns the converted value.
  216. .TP
  217. \fBround(\fIarg\fB)\fR
  218. If \fIarg\fR is an integer value, returns \fIarg\fR, otherwise converts
  219. \fIarg\fR to integer by rounding and returns the converted value.
  220. .PP
  221. In addition to these predefined functions, applications may
  222. define additional functions using \fBTcl_CreateMathFunc\fR().
  223. .VE
  224. .SH "TYPES, OVERFLOW, AND PRECISION"
  225. .PP
  226. All internal computations involving integers are done with the C type
  227. \fIlong\fP, and all internal computations involving floating-point are
  228. done with the C type \fIdouble\fP.
  229. When converting a string to floating-point, exponent overflow is
  230. detected and results in a Tcl error.
  231. For conversion to integer from string, detection of overflow depends
  232. on the behavior of some routines in the local C library, so it should
  233. be regarded as unreliable.
  234. In any case, integer overflow and underflow are generally not detected
  235. reliably for intermediate results.  Floating-point overflow and underflow
  236. are detected to the degree supported by the hardware, which is generally
  237. pretty reliable.
  238. .PP
  239. Conversion among internal representations for integer, floating-point,
  240. and string operands is done automatically as needed.
  241. For arithmetic computations, integers are used until some
  242. floating-point number is introduced, after which floating-point is used.
  243. For example,
  244. .DS
  245. \fBexpr 5 / 4\fR
  246. .DE
  247. returns 1, while
  248. .DS
  249. \fBexpr 5 / 4.0\fR
  250. \fBexpr 5 / ( [string length "abcd"] + 0.0 )
  251. .DE
  252. both return 1.25.
  253. .VS
  254. Floating-point values are always returned with a ``.''
  255. or an ``e'' so that they will not look like integer values.  For
  256. example,
  257. .DS
  258. \fBexpr 20.0/5.0\fR
  259. .DE
  260. returns ``4.0'', not ``4''.  The global variable \fBtcl_precision\fR
  261. determines the the number of significant digits that are retained
  262. when floating values are converted to strings (except that trailing
  263. zeroes are omitted).  If \fBtcl_precision\fR
  264. is unset then 6 digits of precision are used.
  265. To retain all of the significant bits of an IEEE floating-point
  266. number set \fBtcl_precision\fR to 17;  if a value is converted to
  267. string with 17 digits of precision and then converted back to binary
  268. for some later calculation, the resulting binary value is guaranteed
  269. to be identical to the original one.
  270. .VE
  271.  
  272. .SH "STRING OPERATIONS"
  273. .PP
  274. String values may be used as operands of the comparison operators,
  275. although the expression evaluator tries to do comparisons as integer
  276. or floating-point when it can.
  277. If one of the operands of a comparison is a string and the other
  278. has a numeric value, the numeric operand is converted back to
  279. a string using the C \fIsprintf\fP format specifier
  280. \fB%d\fR for integers and \fB%g\fR for floating-point values.
  281. For example, the commands
  282. .DS
  283. \fBexpr {"0x03" > "2"}\fR
  284. \fBexpr {"0y" < "0x12"}\fR
  285. .DE
  286. both return 1.  The first comparison is done using integer
  287. comparison, and the second is done using string comparison after
  288. the second operand is converted to the string ``18''.
  289. .VS
  290. Because of Tcl's tendency to treat values as numbers whenever
  291. possible, it isn't generally a good idea to operators like \fB==\fR
  292. when you really want string comparison and the values of the
  293. operands could be arbitrary;  it's better in these cases to use the
  294. \fBstring compare\fR command instead.
  295. .VE
  296.  
  297. .SH KEYWORDS
  298. arithmetic, boolean, compare, expression
  299.